
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="../../js/layui/css/layui.css">
    <script src="../../js/jquery-3.4.1.js"></script>
    <script src="../../js/layui/layui.all.js"></script>
    <style>
        .container {
            padding: 20px;
        }

        .search-container {
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }

        .search-container input {
            flex: 1;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ced4da;
            margin-right: 10px;
        }

        .search-container button {
            margin-left: 15px;
            padding: 10px 20px;
            border: none;
            background-color: #007bff;
            color: #fff;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;

        }

        .search-container button:hover {
            background-color: #0056b3;
        }

        .role-admin {
            font-weight: bold;
        }

        .status-inactive {
            color: red;
        }

        .operation-btns .layui-btn {
            margin-right: 5px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        table th, table td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #dee2e6;
        }

        table th {
            background-color: #007bff;
            color: #fff;
        }

        table tr:hover {
            background-color: #f1f1f1;
        }

        .user-details {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
            font-size: 16px;
        }

        .user-details .detail-item {
            display: flex;
            justify-content: center;
            width: 100%;
            margin: 10px 0;
        }

        .user-details .detail-item p {
            width: 60%;
            margin: 0 10px;
        }

        .user-details .detail-item strong {
            width: 40%;
            justify-content: center;
            align-content: center;
            display: flex;
        }

        .button-container {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }
        body {
            background-color: #FFF5E1; /* Light peach background */
            color: #6B4226; /* Dark brown text */
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th {
            background-color: #FFD700; /* Warm yellow header background */
            color: #6B4226; /* Dark brown text */
            padding: 10px;
        }
        td {
            background-color: #FFF8DC; /* Light warm background for table cells */
            color: #6B4226; /* Dark brown text */
            padding: 10px;
            border: 1px solid #D2B48C; /* Tan borders */
        }
        a {
            color: #D2691E; /* Warm brown link color */
        }
        button {
            background-color: #FFB347; /* Warm orange button background */
            color: #FFFFFF; /* White text on buttons */
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
        }
        button:hover {
            background-color: #FF7F50; /* Slightly darker orange on hover */
        }
    </style>
</head>
<body>

<div class="container">
    <div class="search-container">
        <label>
            <input type="text" id="searchUsername" class="layui-input" placeholder="请输入用户名搜索">
        </label>
        <button id="searchBtn" type="button">搜索</button>
    </div>
    <table class="layui-table" id="userTable">
        <thead>
        <tr>
            <th>用户ID</th>
            <th>用户名</th>
            <th>邮箱</th>
            <th>角色</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
    <div id="pagination" class="layui-box layui-laypage layui-laypage-default"></div>
</div>
<script>

    layui.use(['table', 'laypage', 'layer'], function () {
        const table = layui.table;
        const laypage = layui.laypage;
        const layer = layui.layer;
        const limit = 5
        let page = 1

        /**
         * 初始化用户列表
         * @param pageNumber  当前页码
         */
        function loadUserTable(pageNumber) {
            let username = $('#searchUsername').val();
            $.ajax({
                url: '/admin/getAllUser',
                method: 'POST',
                data: {
                    page: pageNumber,
                    size: limit,
                    username: username
                },
                success: function (response) {
                    const dataObj = JSON.parse(JSON.stringify(response));

                    let users = dataObj.data;

                    let totalPages = dataObj.count;

                    renderTable(users)
                    renderPagination(totalPages, pageNumber)
                },
                error: function (xhr) {
                    layui.layer.msg('加载用户数据失败', {icon: 2});
                }
            });
        }

        /**
         * 渲染表格
         * @param data
         */
        function renderTable(data) {
            let tbody = $('#userTable tbody');
            tbody.empty();
            data.forEach(function (user) {
                let tr = $('<tr>');
                tr.append('<td>' + user.user_id + '</td>');
                tr.append('<td>' + user.username + '</td>');
                tr.append('<td>' + user.email + '</td>');
                let roleClass = user.role === 'admin' ? 'role-admin' : '';
                tr.append('<td class="' + roleClass + '">' + user.role + '</td>');
                let statusClass = user.status === 'inactive' ? 'status-inactive' : '';
                tr.append('<td class="' + statusClass + '">' + (user.status === 'active' ? '正常' : '已禁用') + '</td>');
                let disClass = user.status === 'inactive' ? 'layui-btn-warm' : 'layui-btn-normal ';
                // status   ENUM ('active', 'inactive')
                tr.append('<td class="operation-btns">' +
                    '<button class="layui-btn layui-btn-primary view-btn" data-id="' + user.user_id + '">查看</button>' +
                    '<button class="layui-btn  ' + disClass + ' disable-btn" data-id="' + user.user_id + '" data-status="' + user.status + '">' + (user.status === 'active' ? '禁用' : '启用') + '</button>' +
                    '<button class="layui-btn layui-btn-danger delete-btn" data-id="' + user.user_id + '">删除</button>' +
                    '</td>');
                tr.data('user', JSON.stringify(user));  // Attach user data to row
                tbody.append(tr);
            });
        }

        /**
         * 渲染分页条
         * @param total
         * @param page
         */
        function renderPagination(total, page) {
            laypage.render({
                elem: 'pagination',
                count: total,
                curr: page,
                limit: limit,
                jump: function (obj, first) {
                    if (!first) {
                        loadUserTable(obj.curr, obj.limit);
                    }
                }
            });
        }

        $(document).ready(function () {
            loadUserTable(page);

            /**
             * 搜索点击事件
             */
            $('#searchBtn').click(function () {
                loadUserTable(1);
            });

            /**
             * 用户详情点击事件
             */
            $(document).on('click', '.view-btn', function () {
                let user = $(this).closest('tr').data('user');


                if (user) {
                    let content = `
               <div class="user-details">
                    <div class="detail-item"><strong>用户名:</strong><p>${user.username}</p></div>
                    <div class="detail-item"><strong>邮箱:</strong><p>${user.email}</p></div>
                    <div class="detail-item"><strong>角色:</strong><p>${user.role}</p></div>
                    <div class="detail-item"><strong>状态:</strong><p>${user.status == 'active' ? '正常' : '已禁用'}</p></div>
                </div>
                <div class="button-container">
                    <button class="layui-btn layui-btn-primary" onclick="layui.layer.closeAll();">关闭</button>
                </div>
    `;
                    layui.layer.open({
                        type: 1,
                        title: '用户详情',
                        content: content,
                        area: ['500px', '400px']
                    });
                } else {
                    layer.msg('无法获取用户详情', {icon: 2});
                }
            });

            /**
             * 禁用 || 启用   用户
             */
            $(document).on('click', '.disable-btn', function () {
                let userId = $(this).data('id');
                let status = $(this).data('status');
                let message, errorMessage;
                if (status === 'active') {
                    message = "确认禁用该用户吗？"
                    errorMessage = "禁用失败，请重试"
                } else {
                    message = "确认启用该用户吗？"
                    errorMessage = "启用失败，请重试"
                }
                layui.layer.confirm(message, function (index) {
                    // 注释掉实际的接口请求
                    $.ajax({
                        url: '/admin/disableOrActiveUser',
                        type: 'POST',
                        data: {userId: userId, status: status},
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                loadUserTable(1);
                                layer.close(index);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },
                        error: function () {
                            layer.msg(errorMessage, {icon: 2});
                        }
                    });
                });
            });

            /**
             * 删除用户
             */
            $(document).on('click', '.delete-btn', function () {
                let userId = $(this).data('id');
                layui.layer.confirm('确定删除这个用户吗？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        url: '/admin/deleteUser',
                        type: 'POST',
                        data: {userId: userId},
                        success: function (res) {
                            layer.msg('删除成功', {icon: 1});
                            loadUserTable(1);
                            layer.close(index);
                        },
                        error: function (xhr) {
                            layer.msg('删除失败', {icon: 2});
                        }
                    });
                });
            });


        });


    });


</script>


</body>
</html>
